/*
function jpAnimate(settings){
    this.t=new Array();
    this.curObj=settings.object;
    this.from=settings.from;
    this.to=settings.to;
    this.useTimems=settings.usetime/5.331900;//单位毫秒
    this.type=settings.speed;//easing减速,add加速,uniform匀速
    this.curUseTime=0;
    this.curUseTime2=0;
    this.ja1=0;
    this.ja2=0;
    this.v0=0;
    this.vt=0;
    this.witch=settings.witch;
    this.olds=0;
    this.jaischange=false;
    this.distance=parseInt(this.to)-parseInt(this.from);
    //通过位移获得加速度
    this.getJa=function(v0,x,t){
        return 2*(x-v0*t)/(t*t);
    }
    //通过初速度末速度时间，计算加速度
    this.getJa2=function(v0,vt,t){
        return (vt-v0)/t;
    }
    //通过初速度和时间和加速度获得末速度
    this.getVt=function(v0,a,t){
        return v0+a*t;
    }
    this.initVar=function(disFen,t,s){
        var a1=2*s/(disFen*t*t);
        var a2=2*s/((disFen-1)*t*t);
        var vt=4*s/(2*disFen*t-2*(disFen-1)*this.t);
        return {ja1:a1,ja2:a2,jvt:vt};
    }
    //通过速度，位移，加速度计算末速度
    this.getVt2=function(v0,s,a){
        return Math.sqrt(2*a*s+v0*v0);
    }
    //通过初速度，加速度，时间，计算位移
    this.getJs=function(v0,a,t){
        return v0*t+0.5*a*t*t;
    }
    //通过初速度 末速度 和加速度计算位移
    this.getJs2=function(v0,vt,a){
        return (vt*vt-v0*v0)/2*a;
    }
    this.start=function(){
        this.selectSpeed();
       
    }
    this.selectSpeed=function(){
         var curobj=this;
        switch(this.type){
            case "easing":
                this.initVar(0.25);
                this.t.push(window.setInterval(function(){curobj.add_sub_speed();},1));
                break;
            case "add":
                this.ja1=this.getJa(0,this.distance,this.useTimems);
                this.t=window.setInterval(function(){curobj.addspeed();},1);
                break;
            case "uniform":
                this.ja1=0;
                this.v0=this.distance/this.useTimems;
                this.t=window.setInterval(function(){curobj.addspeed();},1);
                break;
        }
    }
    this.stop=function(index){
        window.clearInterval(this.t[index]);
    }
    this.setCurObj=function(setObj){
        this.curObj=setObj;
    }
    this.css=function(property,value){
        this.curObj.style[property]=value;
    }

    this.add_sub_speed=function(){
        var vtt=this.getVt(this.v0,this.ja1,this.curUseTime);
        var s=0;
        var vtt2=0;
        if(vtt>this.vt){
            if(this.jaischange==false){
                
                this.useTimems=this.useTimems-this.curUseTime;
                this.jaischange=true;
            }
            vtt2=this.getVt(this.vt,this.ja2,this.curUseTime2);
            s=this.getJs(this.vt,this.ja2,this.curUseTime2);
            s=this.olds+s;
            this.curUseTime2+=1;
        }else{
            s=this.getJs(this.v0,this.ja1,this.curUseTime);
            this.olds=s+this.from;
            s=this.from+s;
        }
        
       this.css(this.witch,s);
        this.curUseTime+=1;
        if(this.useTimems<=this.curUseTime2){
            this.jaischange=false;
            this.stop();
            this.css(this.witch,this.to);
        }
    }
    this.addspeed=function(){
         if(this.curUseTime<=this.useTimems){
           var s=this.getJs(this.v0,this.ja1,this.curUseTime);
           this.olds=s;
           this.curUseTime+=1;
           var pos=s+this.from;
          
           this.css(this.witch,pos);
         
       }else{
           this.css(this.witch,this.to);
           this.jaischange=false;
           this.stop();
      
       }
      
    }

}
*/
jP=function(id){    
    return new jPClass(id);
   
}
jPClass=function(id){
    this.selected=false;//元素是否被选中
    this.curObject=document.getElementById(id);
    this.tagName=this.curObject.tagName;
    jPClass.objectList=jPClass.objectList?jPClass.objectList:new Array();
    this.index=jPClass.objectList.push(this)-1;
    var curObject=this;
    this.shanT=0;
    this.aT=0;
    this.mask;
    this.coord={x:100,y:100};
    this.finishx=false;
    this.finishy=false;
    this.allT=new Object();
    this.html=function(){
        var value = arguments[0] ? arguments[0] : false; 
        if(value!=false){
            this.curObject.innerHTML=value;
            return this;
        }else{
            return  this.curObject.innerHTML;
        }
        
    };
    this.Bind=function(evt,func){
        if(window.attachEvent){
            this.curObject.attachEvent("on"+evt,func);
        }else{
        this.curObject.addEventListener(evt,func);
        }
        return this;
    };
    this.createMask=function(parentObj){
       parentObj.style.position="relative";
       var mask=document.createElement("div");
       this.css(mask,"position","absolute");
       this.css(mask,"backgroundColor","#1c1c1c");
       this.css(mask,"width","100%");
       this.css(mask,"height","100%");
       this.css(mask,"top","0px");
       this.css(mask,"left","0px");
       this.opacity(mask,30);
       parentObj.appendChild(mask);
       return mask;
    };
    this.removeMask=function(maskNode){
        if(maskNode&&maskNode.parentNode){
            maskNode.parentNode.removeChild(maskNode);
        }
    };
    //闪动边框
     this.borderShan=function(){
       var obj=this.curObject;
       var red=0;
       this.getWidth(obj);
      // this.moveAction(obj,"easing",5000,{x:0,y:0},{x:100,y:100},{x:100,y:400},{x:0,y:400},{x:700,y:400},{x:700,y:100});
       this.mask=this.createMask(obj);
       this.shanT=window.setInterval(function(){if(red<255){red+=1;}else{red=0;}obj.style.border="1px dashed rgb("+red+","+red+",0)";},1);
    };
    //获得对象宽度
    this.getWidth=function(obj){
        return obj.offsetWidth;
    };
    //获得对象高度
    this.getHeight=function(obj){
        return obj.offsetHeight;
    };
    //获得当前对象的x坐标
    this.getPosX=function(obj){
        var x=obj.offsetLeft-this.coord.x;
        return x;
    };
    //获得当前对象的y坐标
    this.getPosY=function(obj){
        var y=obj.offsetTop-this.coord.y;
        return y;
    };
    //获得当前对象的坐标点
    this.getPos=function(obj){
        var _x=this.getPosX(obj);
        var _y=this.getPosY(obj);
        return {x:_x,y:_y};
    };
    this.setPosX=function(obj,x){
        var _x=x+this.coord.x;
        this.css(obj,"left",_x+"px");
        return this;
    };
    this.setPosY=function(obj,y){
      var _y=y+this.coord.y;
      this.css(obj,"top",_y+"px");
      return this;
    };
    //设置坐标点
    this.setPos=function(obj,pos){
        this.setPosX(obj,pos.x).setPosY(obj,pos.y);
        return this;
    };
    //通过传递字符x或y,设置坐标点
    this.setPosEx=function(obj,type,dis){
      
        switch(type){
            case "x":
                this.setPosX(obj,dis);
                break;
            case "y":
                this.setPosY(obj,dis);
                break;
        }
    };
    //选择运动模式,参数1：执行动画的对象，参数2：模式，参数3：所花费时间，参数4：初始速度对象信息，参数5：起始点的坐标，参数6：终止点的坐标,参数7：当做easing模式时候,此参数为路程的几分之几开始做减速
    this.selectMode=function(obj,mode,speed,info,pos1,pos2){
        var objInfo=new Object();
        switch(mode){
            case "easing":
                var fen=arguments[6]?arguments[6]:0.5;
                objInfo["x"]=this.easingSu(obj,fen,"x",speed,info.x.v0,pos1.x,pos2.x);
                objInfo["y"]=this.easingSu(obj,fen,"y",speed,info.y.v0,pos1.y,pos2.y);
           
                break;
            case "add":
                objInfo["x"]=this.jiaSu(obj,"x",speed,info.x.v0,pos1.x,pos2.x);
                objInfo["y"]=this.jiaSu(obj,"y",speed,info.y.v0,pos1.y,pos2.y);
                break;
            case "uniform":
                objInfo["x"]=this.yunSu(obj,"x",speed,pos1.x,pos2.x);
                objInfo["y"]=this.yunSu(obj,"y",speed,pos1.y,pos2.y);
                break;
        }
        return objInfo;
    };
    //参数1：执行动画的对象,参数2：要在几分之几的路程点开始减速,参数3：做运动的是x坐标还是y坐标,参数4：从哪,参数5：到哪
    this.easingSu=function(obj,fen,propty,_t,_v0,from,to){
        var dis=to-from;
        var thisClass=this;
        var jiaSuInfo=this.initVar(fen,_t,dis);
        var t1=0;
        var t2=0;
        var st=_t;
        var pos=0;
       
        var ret={s:dis,v0:_v0,t:_t,vt:jiaSuInfo.jvt,a:0};
        if(dis!=0){
            var _time=window.setInterval(function(){
                var vtt=thisClass.getVt(_v0,jiaSuInfo.ja1,t1);
                
                if(Math.abs(vtt)>Math.abs(jiaSuInfo.jvt)){
                    if(t2+t1>st){
                        thisClass.setPosEx(obj,propty,to);
                        thisClass.setFinishFlg(propty);
                        thisClass.clearT(_time);
                    }else{
                        var s=thisClass.getJs(jiaSuInfo.jvt,jiaSuInfo.ja2,t2);
                        var ss=parseFloat(s)+parseFloat(pos);
                        thisClass.setPosEx(obj,propty,ss);
                        t2+=1;
                    }
                }else{
                    var s=thisClass.getJs(_v0,jiaSuInfo.ja1,t1);
                    pos=s+from;
                    thisClass.setPosEx(obj,propty,pos);
                    t1=t1+1;
                }
                
            },1);
            this.allT[_time]=_time;
        }else{
            thisClass.setFinishFlg(propty);
        }
        return ret;
    };
   
    this.yunSu=function(obj,propty,_t,from,to){
        var dis=to-from;
        var thisClass=this;
        var v=dis/_t;
        var ret={s:dis,v0:v,t:_t,vt:v,a:0};
        curt=0;
        if(dis!=0){
            var _time=window.setInterval(function(){
                if(curt<=_t){
                    var pos=v*curt+from;
                    thisClass.setPosEx(obj,propty,pos);
                    curt++;
                }else{
                    thisClass.setPosEx(obj,propty,to);
                    thisClass.setFinishFlg(propty);
                    thisClass.clearT(_time);
                }
            },1);
            thisClass.allT[_time]=_time;
        }else{
          
            thisClass.setFinishFlg(propty);
        }
        
        return ret;
    }
    this.parseTime=function(_t){
         var type=this.browserType();
         switch(type){
         case 1:
             return _t/5.131900;
         case 2:
             return _t/4.1170;
         case 3:
             return _t/14.151900;
             
         }
    };
    //判断是否为该浏览器
    this.browserType=function(){
        var agent=window.navigator.userAgent.toLowerCase();
        if(agent.indexOf("chrome")>=0){
            return 1;
        }else if(agent.indexOf("firefox")>=0){
            return 2;
        }
        else if(agent.indexOf("msie")>=0){
           return 3;
        }
        return 1;
    };
    //计算所走的总位移
    this.zongWeiYi=function(posList){
        var sum=0;
        var start=posList.pop();
        var next=posList.pop();
        while(next){
           var s=this.jiSuanWeiYi(start,next);
            sum+=s;
            start=next;
            next=posList.pop();
        }
   
           return sum;
    };
    //计算时间比
    this.shiJianBi=function(s,s1,_t){
        return _t*s1/s;
    }
    this.jiaSu=function(obj,propty,_t,_v0,from,to){
        var dis=to-from;
       
        var thisClass=this;
        
  
        var _a=this.getJa(_v0,dis,_t);
        var ret={s:dis,v0:_v0,t:_t,vt:this.getVt(_v0,_a,_t),a:_a};

        var curt=0;
        if(dis!=0){
      
        var _time=window.setInterval(function(){
             if(curt<=_t){
                    var s=thisClass.getJs(_v0,_a,curt);
                    ret.vt=thisClass.getVt(_v0,_a,curt);
           
                    curt+=1;
                    var pos=s+from;
                    thisClass.setPosEx(obj,propty,pos);

                }else{
                    thisClass.setPosEx(obj,propty,to);
                    thisClass.setFinishFlg(propty);
                    thisClass.clearT(_time);

                }
        },1);
       this.allT[_time]=_time;
        }else{
             thisClass.setFinishFlg(propty);
             ret={s:dis,v0:0,t:0,vt:0,a:0};
        }
       
       return ret;
    }; 
    this.setFinishFlg=function(propty){
        switch(propty){
            case "x":
                this.finishx=true;
                break;
            case "y":
                this.finishy=true;
                break;
        }
    };
    this.jiSuanWeiYi=function(from,to){
         var x=Math.abs(to.x-from.x);
         var y=Math.abs(to.y-from.y);
         var s=Math.sqrt(x*x+y*y);
         return s;
    };
    //动画，参数1：要做动画的对象,参数2:运动模式，加速,匀速等,参数3：动画完成的时间，参数4：后面的都是坐标点，动画运动的轨迹
    this.moveAction=function(obj,mode,speed){
        speed=this.parseTime(speed);
        var posList=new Array();
        var argcount=arguments.length;
        for(var i=argcount-1;i>2;i--){
            posList.push(arguments[i]);
        }
        var clonePostList=posList.concat();
        var zongweiyi=this.zongWeiYi(clonePostList);
       
     
        
        var startpos=posList.pop();
        this.setPos(obj,startpos);
        var thisClass=this;
        var nextpos=posList.pop();
        var info=new Object();
        info["x"]={s:0,v0:0,t:0,vt:0,a:0};
        info["y"]={s:0,v0:0,t:0,vt:0,a:0};
        var s1=this.jiSuanWeiYi(startpos,nextpos);
        var uset=this.shiJianBi(zongweiyi,s1,speed);

        info=this.selectMode(obj,mode,uset,info,startpos,nextpos);
        var _t=window.setInterval(function(){
           
                if(thisClass.finishx&&thisClass.finishy){
                    thisClass.finishx=false;
                    thisClass.finishy=false;
                    startpos=nextpos;
                    nextpos=posList.pop();
                    
                    if(nextpos){
                      
                      info.x.v0=0;
                      info.y.v0=0;
                      s1=thisClass.jiSuanWeiYi(startpos,nextpos);
                      uset=thisClass.shiJianBi(zongweiyi,s1,speed);
                 
                      info=thisClass.selectMode(obj,mode,uset,info,startpos,nextpos);
                    }else{
                      thisClass.clearT(_t);  
                    }
                 
                }
         
        },1);
        this.allT[_t]=_t;
        return this;
    };
    //设置样式或获取样式
    this.css=function(obj,property){
       
        var value = arguments[2] ? arguments[2] : false; 
        if(value!=false){
          obj.style[property]=value;
        }else{
            return obj.style[property];
        }
    };
    //设置或获取透明度
    this.opacity=function(obj){
        var value = arguments[1] ? arguments[1] : false; 
        try{
            if(value!=false){
            this.css(obj,"filter","alpha(opacity="+value+")");
            this.css(obj,"opacity",value/100);
            }else{
                var filter=this.css(obj,"filter");
                if(filter){
                    return filter;
                }else{
                    return this.css(obj,"opacity")*100;
                }
            }
           
        }catch(e){
           
        }
    };
    this.clearShanT=function(){
        window.clearInterval(this.shanT);
    };
    
    //通过位移获得加速度
    this.getJa=function(v0,x,t){
        return 2*(x-v0*t)/(t*t);
    };
    //通过位移，
    //通过初速度末速度时间，计算加速度
    this.getJa2=function(v0,vt,t){
        return (vt-v0)/t;
    };
    //通过初速度和时间和加速度获得末速度
    this.getVt=function(v0,a,t){
        return v0+a*t;
    };
    //先加速后减速获得所需要的参数。参数1：距离的几分之几开始减速,参数2：这段路程所花费的时间,参数3：这段路程的距离
    //返回值：加速度1，加速度2，达到多少末速度开始减速
    this.initVar=function(disFen,t,s){
        var a1=2*s/(disFen*t*t);
        var a2=2*s/((disFen-1)*t*t);
        var vt=4*s/(2*disFen*t-2*(disFen-1)*t);
        return {ja1:a1,ja2:a2,jvt:vt};
    };
    //通过速度，位移，加速度计算末速度
    this.getVt2=function(v0,s,a){
        return Math.sqrt(2*a*s+v0*v0);
    };
    //通过初速度，加速度，时间，计算位移
    this.getJs=function(v0,a,t){
        return v0*t+0.5*a*t*t;
    };
    //通过初速度 末速度 和加速度计算位移
    this.getJs2=function(v0,vt,a){
        return (vt*vt-v0*v0)/2*a;
    };
    
    this.clearT=function(t){
        delete this.allT[t];
        window.clearInterval(t);
    };
   
    
    //当鼠标点击时候，该对象被选中
    this.Bind("click",function(){
        for(var i in jPClass.objectList){
            jPClass.objectList[i].selected=false;
            jPClass.objectList[i].clearShanT();
            jPClass.objectList[i].curObject.style.border="none";
            jPClass.objectList[i].removeMask(jPClass.objectList[i].mask);
        }
        curObject.selected=true;
        curObject.borderShan();
    });
   
    
};

